package cn.kgc.springmvc03.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @Author: 课工场
 * @Version: v1.0  2023/5/8
 * @Description:  拦截器    拦截请求
 */
public class LoginIntercepter  implements HandlerInterceptor {

    // 请求到达目标handler之前先执行    boolean true  放行
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // session  -> username
        System.out.println("===============LoginIntercepter==================");
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");

        if(username==null){  //  非法用户
            response.sendRedirect("/login.jsp");
            return false;
        }
        return true;
    }
}
